package com.micangcaihang.micang.util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/8/9
 *     update : 2017/8/9
 *     desc   : MiCang
 *     version: 1.0
 * </pre>
 */
public class HttpUtil {
    public static Map<String, String> objectToMapStringArray(Object obj, String type) {
        Map<String, String> map = new HashMap<String, String>();
        Class<?> c = null;
        try {
            c = Class.forName(obj.getClass().getName());
            Method[] m = c.getMethods();
            for (int i = 0; i < m.length; i++) {
                String method = m[i].getName();
                if (method.startsWith("get") && !method.equals("getClass")) {
                    try {
                        String value = (String) m[i].invoke(obj);
                        if (value != null) {
                            String key = method.substring(3);
                            key = key.substring(0, 1).toLowerCase() + key.substring(1);
                            map.put(type + "[" + key + "]", value);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
